package com.example.GeoCoder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpRequestFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



import android.app.Activity;
import android.app.ListActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class GeoCoderActivity extends ListActivity {

	String KEY = "AIzaSyDzCR523KK9HS6_iKvKqbsTyJ1JP4-BeUM";
	String address = "-33.8670522,151.1957362";
	String radius= "500";
	String types="food";
	String name = "harbour";
	String sensor = "false";
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		JSONObject json = getLocationInfo(address,radius,types,name,sensor,KEY);
		ArrayAdapter<PlaceObject> adapter = new MyListAdapter(this,getList(json));
		setListAdapter(adapter);
	}

	private List<PlaceObject> getList(JSONObject json) {
		// TODO Auto-generated method stub
        try {
        	
        	JSONArray results = json.getJSONArray("results");
        	List<PlaceObject> list = new ArrayList<PlaceObject>();
        	PlaceObject loc;
        	for(int i= 0; i < results.length(); i++) {
        		JSONObject jsonCurLocation = results.getJSONObject(i);
        	    loc= new PlaceObject(jsonCurLocation);
        	   list.add(i, loc);
        	}
        	return list;
        } catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
	public static JSONObject getLocationInfo(String location,String radius, String types, String name, String sensor,String key) {
		final String PLACES_AUTOCOMPLETE_URL = "https://maps.googleapis.com/maps/api/place/autocomplete/json?";
		//HttpGet httpGet = new HttpGet("http://maps.googleapis.com/maps/api/geocode/json?address="+address+"ka&sensor=false");
		HttpGet httpGet = new HttpGet("https://maps.googleapis.com/maps/api/place/search/json?location="+location+"&radius="+radius+"&types="+types+"&name="+name+"&sensor="+sensor+"&key=AIzaSyDzCR523KK9HS6_iKvKqbsTyJ1JP4-BeUM");
		HttpClient client = new DefaultHttpClient();

		   
		HttpResponse response;
		StringBuilder stringBuilder = new StringBuilder();

		try {
			response = client.execute(httpGet);
			
			HttpEntity entity = response.getEntity();
			InputStream stream = entity.getContent();
			int b;
			while ((b = stream.read()) != -1) {
				stringBuilder.append((char) b);
			}
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}

		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject = new JSONObject(stringBuilder.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return jsonObject;
	}
}
	
	


